package com.dhq.util;

import java.util.Random;

/**
 * 测试ThreadLocl类存放简单数据类型
 */
public class ThreadLocalSimpleDataTest {
    private static ThreadLocal<Integer> threadLocal=new ThreadLocal<>();

    public static void main(String[] args) {
        for (int i = 0; i <2 ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int random=new Random().nextInt();
                    threadLocal.set(random);
                    System.out.println(Thread.currentThread().getName()+": "+threadLocal.get());
                    A.get();
                    B.get();

                }
            }).start();
        }
    }

    static class A{
        public static void get(){
            int a =threadLocal.get();
            System.out.println("A == "+Thread.currentThread().getName()+": "+a);
        }
    }

    static class B{
        public static void get(){
            int a =threadLocal.get();
            System.out.println("B == "+Thread.currentThread().getName()+": "+a);
        }
    }
}
